package com.vhr.service.impl;

import com.vhr.VO.AgeCountVO;
import com.vhr.VO.EmployeeVO;
import com.vhr.entities.*;
import com.vhr.form.EmployeeForm;
import com.vhr.mapper.AccountMapper;
import com.vhr.mapper.ContractMapper;
import com.vhr.mapper.EmployeeMapper;
import com.vhr.mapper.WelfareMapper;
import com.vhr.service.IEmployeeService;
import com.vhr.util.MonthUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Name EmployeeServiceImpl
 * @Description:
 * @Author: CHAO
 * @Date: 2019/9/23
 */
@Service
@Slf4j
public class IEmployeeServiceImpl implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ContractMapper contractMapper;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private WelfareMapper welfareMapper;


    @Override
    public int insertWelfare(Welfare welfare) {
        return welfareMapper.insert(welfare);
    }

    @Override
    public int updateWelfareByEmpId(Welfare welfare, Employee employee) {
        Employee emp = employeeMapper.selectByPrimaryKey(employee.getEmpId());
        welfare.setWelfareId(emp.getWelfateId());
        return welfareMapper.updateByPrimaryKeySelective(welfare);
    }

    @Override
    public EmployeeForm selectEditPage(Integer empId) {
        return employeeMapper.selectEditPage(empId);
    }

    @Override
    public List<EmployeeVO> selectByDeptAndStatusAndName(Integer deptId, Integer status,String empName) {
        List<EmployeeVO> list = employeeMapper.selectByDeptAndStatusAndName(deptId, status,empName);
        return list;
    }

    //查询所有员工
    @Override
    public List<EmployeeVO> selectAll() {
        return employeeMapper.selectAll();
    }

    //根据主键查询员工
    @Override
    public Employee selectEmployeeByPrimaryKey(Employee emp) {
        return employeeMapper.selectByPrimaryKey(emp.getEmpId());
    }

    //根据其他条件查询员工信息
    @Override
    public List<Employee> selectEmployeeByExample(Employee emp) {
        EmployeeExample employeeExample = new EmployeeExample();
        EmployeeExample.Criteria criteria = employeeExample.createCriteria();
        //判断emp中有哪些数据，然后根据这些数据进行查询
        if(emp.getDeptId()!=null) criteria.andEmpIdEqualTo(emp.getEmpId());
        if(emp.getEmpBirth()!=null) criteria.andEmpBirthEqualTo(emp.getEmpBirth());
        if(emp.getEmpEduc()!=null) criteria.andEmpEducEqualTo(emp.getEmpEduc());
        if(emp.getEmpEntrytime()!=null) criteria.andEmpEntrytimeEqualTo(emp.getEmpEntrytime());
        if(emp.getEmpMail()!=null) criteria.andEmpMailEqualTo(emp.getEmpMail());
        if(emp.getEmpName()!=null) criteria.andEmpNameEqualTo(emp.getEmpName());
        if(emp.getEmpNation()!=null) criteria.andEmpNationEqualTo(emp.getEmpName());
        if(emp.getEmpNative()!=null) criteria.andEmpNativeEqualTo(emp.getEmpNative());
        if(emp.getEmpPhone()!=null) criteria.andEmpPhoneEqualTo(emp.getEmpPhone());
        if(emp.getEmpStatus()!=null) criteria.andEmpStatusEqualTo(emp.getEmpStatus());
        if(emp.getEmpType()!=null) criteria.andEmpTypeEqualTo(emp.getEmpType());
        List<Employee> employees = employeeMapper.selectByExample(employeeExample);
        return employees;
    }

    //根据员工主键更新员工信息
    @Override
    public int updateEmployeeByPrimaryKey(Employee emp) {

        emp.setEmpBirth(MonthUtil.MonthDecrease(emp.getEmpBirth()));
        return employeeMapper.updateByPrimaryKeySelective(emp);
    }


    //添加员工
    @Transactional
    @Override
    public int insertEmployee(Employee emp) {
        //插入员工信息
        int result = employeeMapper.insert(emp);
        //插入员工账号信息
        //添加员工时需为员工创建账号，账号自动生成策略 ，账号密码默认123
        Integer max;
        if((max=employeeMapper.selectMaxId())==null){
            max=1001;
        }else{
            max=max+1;
        }
        Account account = new Account();
        account.setEmpNum(max);
        account.setEmpId(emp.getEmpId());
        account.setEmpPassword("123");
        result=result+accountMapper.insert(account);
        return result;
    }

    @Transactional
    @Override
    public int delelteEmployee(Employee emp) {
        //0在职，1待离职，2已离职
        emp.setEmpStatus(2);
        return employeeMapper.updateByPrimaryKeySelective(emp);
    }

    //根据员工id查询员工的合同
    @Override
    public Contract selectContractByEmployee(Employee emp) {
        ContractExample contractExample = new ContractExample();
        contractExample.createCriteria().andEmpIdEqualTo(emp.getEmpId());
        List<Contract> contracts = contractMapper.selectByExample(contractExample);
        if(contracts==null){
            return null;
        }
        return contracts.get(0);
    }

    //根据员工信息更新合同
    @Transactional
    @Override
    public int updateContractByEmployee(Employee emp,Contract contract) {
        ContractExample contractExample = new ContractExample();
        contractExample.createCriteria().andEmpIdEqualTo(emp.getEmpId());
        int result = contractMapper.updateByExampleSelective(contract, contractExample);
        return result;
    }

    //插入合同信息
    @Override
    public int insertContract(Contract contract) {
        //日期中月份减1
        contract.setContractTime(MonthUtil.MonthDecrease(contract.getContractTime()));
        return contractMapper.insert(contract);
    }

    //登录
    @Override
    public Employee login(Account account) {
        AccountExample example = new AccountExample();
        example.createCriteria().andEmpNumEqualTo(account.getEmpNum()).andEmpPasswordEqualTo(account.getEmpPassword());
        List<Account> list = accountMapper.selectByExample(example);
        System.out.println(list.size());
        if(list.size()==1){
           return employeeMapper.selectByPrimaryKey(list.get(0).getEmpId());
       }
        return null;
    }

    //退出
    @Override
    public Void logout() {
        return null;
    }

    //修改密码
    @Transactional
    @Override
    public int changePassword(Account account, Employee emp) {
        AccountExample accountExample = new AccountExample();
        accountExample.createCriteria().andEmpIdEqualTo(emp.getEmpId());
//        log.info("【修改密码】 员工号= }"+emp.getEmpId());
        int  result= accountMapper.updateByExampleSelective(account, accountExample);
        return result;
    }

    //根据员工的id查询员工的账户
    @Override
    public Account selectByEmployee(Employee emp) {
        AccountExample accountExample = new AccountExample();
        accountExample.createCriteria().andEmpIdEqualTo(emp.getEmpId());
        List<Account> accounts = accountMapper.selectByExample(accountExample);
        return accounts.get(0);
    }

    //统计员工年龄信息
    @Override
    public List<AgeCountVO> countByEmpBirth() {
        List<AgeCountVO> ageCountVOList = new ArrayList<AgeCountVO>();
        Integer[] counts = {18,25,26,30,31,35,36,40,41,45,46,50,51,60};
        String[] ageStage = {"18-25","26-30","31-35","36-40","41-45","46-50","51以上"};
        Integer count = 0;
        List<Integer>  list = new ArrayList<Integer>();

        for(int i=0;i<counts.length;i++){
            int result = employeeMapper.countByEmpBirth(counts[i],counts[i+1]);
            list.add(result);
            i++;
            AgeCountVO ageCountVO = new AgeCountVO(ageStage[count],result);
            ageCountVOList.add(ageCountVO);
            count++;
        }
        return ageCountVOList;
    }

    //统计员工在职信息
    @Override
    public List<Integer> countByEmpStatus() {
        Integer[] condition = new Integer[3];
        List<Integer> results = new ArrayList<Integer>();
        for(int i=0;i<condition.length;i++){
            EmployeeExample employeeExample = new EmployeeExample();
            employeeExample.createCriteria().andEmpStatusEqualTo(i);
            Integer result = employeeMapper.countByExample(employeeExample);
            results.add(result);
        }
        return results;
    }

    //统计员工司龄信息
    @Override
    public List<Integer> countByContractDuration() {
        Integer[] counts = new Integer[5];
        ContractExample contractExample = new ContractExample();
        Calendar cal = Calendar.getInstance();
        cal.setTime(Calendar.getInstance().getTime());
        //存放员工司龄信息结果
        List<Integer> results = new ArrayList<Integer>();

        for(int i=0;i<counts.length;i++){
            Date date = cal.getTime();
            //清除上一次contractExample List中的查询条件
            contractExample.clear();
            ContractExample.Criteria criteria = contractExample.createCriteria();
            // System.out.println("减之前###："+cal.getTime());
            //获取上一年
            cal.add(Calendar.YEAR,-1);
            // System.out.println("减之后###："+cal.getTime());

            criteria.andContractTimeBetween(cal.getTime(),date);
            int result = contractMapper.countByExample(contractExample);

            results.add(result);
        }
        contractExample.clear();
        contractExample.createCriteria().andContractTimeLessThan(cal.getTime());
        results.add(contractMapper.countByExample(contractExample));
        return results;
    }

    //筛选列表员工名字
    @Override
    public List<Employee> searchEmployeeName() {

        EmployeeExample employeeExample = new EmployeeExample();
        employeeExample.setDistinct(false);
        EmployeeExample.Criteria criteria = employeeExample.createCriteria();

        List<Employee> list = employeeMapper.selectByExample(null);
        return list;
    }

}


